﻿using OrderControl;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderControlWinForm {
    public partial class Form_Display : Form {
        public Form_Display() {
            InitializeComponent();
        }

        private void Form_Display_Load(object sender, EventArgs e) {
            OrderService.Import();
            bindingSource_Order.DataSource = OrderService.Instance.Orders;
            toolStripComboBox_SearchType.SelectedIndex = 0;
        }

        private void dataGridView_Order_RowEnter(object sender, DataGridViewCellEventArgs e) {
            bindingSource_OrderDetail.DataSource = bindingSource_Order[e.RowIndex];
        }

        private void ToolStripMenuItem_Import_Click(object sender, EventArgs e) {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "xml files (*.xml)|*.xml";
            fileDialog.RestoreDirectory = true;
            if (fileDialog.ShowDialog() == DialogResult.OK) {
                OrderService.Import(fileDialog.FileName);
                bindingSource_Order.DataSource = OrderService.Instance.Orders;
                if (OrderService.Instance.Orders.Any()) {
                    bindingSource_OrderDetail.DataSource = bindingSource_Order[0];
                }
            }
        }

        private void ToolStripMenuItem_Export_Click(object sender, EventArgs e) {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Filter = "xml files (*.xml)|*.xml";
            fileDialog.RestoreDirectory = true;
            if (fileDialog.ShowDialog() == DialogResult.OK) {
                OrderService.Export(fileDialog.FileName);
            }
        }

        private void toolStripButton_Add_Click(object sender, EventArgs e) {
            Order order;
            while (true) {
                Form_Order form_Order = new Form_Order();
                if (form_Order.ShowDialog() == DialogResult.OK) {
                    try {
                        order = form_Order.UsingOrder;
                        OrderService.Instance.Add(order);
                    } catch (OrderAddException) {
                        if (MessageBox.Show("Identical order id is not allowed!", "Order", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation) == DialogResult.Retry) {
                            continue;
                        } else {
                            break;
                        }
                    }
                }
                break;
            }
            bindingSource_Order.ResetBindings(false);
        }

        private void toolStripButton_Delete_Click(object sender, EventArgs e) {
            if (OrderService.Instance.Orders.Any()) {
                OrderService.Instance.Delete((int)dataGridView_Order[0, dataGridView_Order.CurrentCell.RowIndex].Value);
                if (!OrderService.Instance.Orders.Any()) {
                    bindingSource_OrderDetail.Clear();
                }
                bindingSource_Order.ResetBindings(false);
            }
        }

        private void toolStripButton_Edit_Click(object sender, EventArgs e) {
            if (OrderService.Instance.Orders.Any()) {
                Order order = OrderService.Instance.Orders[dataGridView_Order.CurrentCell.RowIndex];
                OrderService.Instance.Delete(order.Id);
                bindingSource_Order.ResetBindings(false);
                while (true) {
                    Form_Order form_Order = new Form_Order(order);
                    form_Order.ShowDialog();
                    try {
                        order = form_Order.UsingOrder;
                        OrderService.Instance.Add(order);
                    } catch (OrderAddException) {
                        MessageBox.Show("Identical order id is not allowed!", "Order", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        continue;
                    }
                    break;
                }
                bindingSource_Order.ResetBindings(false);
                bindingSource_OrderDetail.ResetBindings(false);
            }
        }

        private void toolStripButton_Search_Click(object sender, EventArgs e) {
            bool isRightFormat = true;
            string inputText = toolStripTextBox_SearchInput.Text;
            try {
                switch (toolStripComboBox_SearchType.SelectedIndex) {
                    case 0:
                        if (isRightFormat = Regex.IsMatch(inputText, "^[0-9]+$")) {
                            bindingSource_Order.DataSource = OrderService.Instance.Search(OrderService.SearchType.Id, int.Parse(inputText));
                        }
                        break;
                    case 1:
                        bindingSource_Order.DataSource = OrderService.Instance.Search(OrderService.SearchType.Commodity, inputText);
                        break;
                    case 2:
                        bindingSource_Order.DataSource = OrderService.Instance.Search(OrderService.SearchType.Customer, inputText);
                        break;
                    case 3:
                        if (isRightFormat = Regex.IsMatch(inputText, "^[0-9]+([.][0-9]*)?$")) {
                            bindingSource_Order.DataSource = OrderService.Instance.Search(OrderService.SearchType.PriceHigher, double.Parse(inputText));
                        }
                        break;
                    case 4:
                        if (isRightFormat = Regex.IsMatch(inputText, "^[0-9]+([.][0-9]*)?$")) {
                            bindingSource_Order.DataSource = OrderService.Instance.Search(OrderService.SearchType.PriceLower, double.Parse(inputText));
                        }
                        break;
                }
                if (!isRightFormat) {
                    if (MessageBox.Show("Error format!", "Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) {
                        toolStripTextBox_SearchInput.Text = "";
                    }
                } else {
                    toolStripButton_Reset.Enabled = true;
                }
            } catch (OrderSearchException exception) {
                if (MessageBox.Show(exception.Message, "Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) {
                    toolStripTextBox_SearchInput.Text = "";
                }
            }
        }

        private void toolStripButton_Reset_Click(object sender, EventArgs e) {
            bindingSource_Order.DataSource = OrderService.Instance.Orders;
            toolStripButton_Reset.Enabled = false;
        }
    }
}
